/**
 * AgaEngine (c)2011.
 * FileName:Rectangle.java
 */
package aga.engine.physic;

import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import aga.engine.AgaCfg;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;

/**
 * 
 * @author User
 */
public class Rectangle extends PhysicBody {
    //===========================================================
    //Constants
    //===========================================================

    //===========================================================
    //Fields
    //===========================================================
    private float halfW;
    private float halfH;

    public int color = 0;

    private Paint paint = new Paint();
    private Paint paintBorder = new Paint();
    private Matrix matrix = new Matrix();

    //===========================================================
    //Constructors
    //===========================================================

    protected Rectangle(World world, float x, float y, float halfWidth,
            float halfHeight, float density, float friction, float restitution, BodyType bt, int color) {
        super(world);

        this.color = color;
        if (this.color != 0) {
            paint.setColor(color);
            paint.setAlpha(50);

            paintBorder.setColor(color);
            paintBorder.setStyle(Style.STROKE);
        } else if (bt == BodyType.DYNAMIC) {
            paint.setColor(Color.GREEN);
            paint.setAlpha(50);

            paintBorder.setColor(Color.GREEN);
            paintBorder.setStyle(Style.STROKE);
            paintBorder.setAntiAlias(true);
        } else {
            paint.setColor(Color.BLUE);
            paint.setAlpha(50);

            paintBorder.setColor(Color.BLUE);
            paintBorder.setStyle(Style.STROKE);
        }


        PolygonShape polygonDef = new PolygonShape();
        polygonDef.setAsBox(halfWidth, halfHeight);

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = polygonDef;
        fixtureDef.density = density;
        fixtureDef.friction = friction;
        fixtureDef.restitution = restitution;

        halfW = halfWidth;
        halfH = halfHeight;

        bodyDef.position.x = x;
        bodyDef.position.y = y;
        bodyDef.type = bt;
        body = world.createBody(bodyDef);
        body.createFixture(fixtureDef);


    }


    //===========================================================
    //Getter & Setter
    //===========================================================




    //===========================================================
    //Methods for/from SuperClass/Interfaces
    //===========================================================


    @Override
    public void draw(Canvas canvas) {
        RectF rectf = new RectF(
                (getX()-halfW)*AgaCfg.PHY2MAP_SCALE,
                (getY()-halfH)*AgaCfg.PHY2MAP_SCALE,
                (getX()+halfW)*AgaCfg.PHY2MAP_SCALE,
                (getY()+halfH)*AgaCfg.PHY2MAP_SCALE );

        canvas.save();

        matrix.reset();
        matrix.preRotate(getAngle(), getX()*AgaCfg.PHY2MAP_SCALE, getY()*AgaCfg.PHY2MAP_SCALE);
        canvas.setMatrix(matrix);
        canvas.drawRect(rectf, paint);
        canvas.drawRect(rectf, paintBorder);

        canvas.restore();
    }

    //===========================================================
    //Methods
    //===========================================================

    //===========================================================
    //Inner and Anonymous Classes
    //===========================================================

    @Override
    public void destroy() {
        mWorld.destroyBody(body);
    }
}
